﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using ZXing; 
using ZXing.Rendering;
using ZXing.Windows.Compatibility;
namespace StorageApp
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class QRCodeDialog : Window
    {
        public QRCodeDialog()
        {
            initQrDlg();
        }
        public void initQrDlg()
        {
            InitializeComponent();
            this.ShowInTaskbar = false; // 不在任务栏显示窗口  
            this.Deactivated += PopupWindow_Deactivated; // 监听窗口失去焦点的事件  
        }
        public void setBarCode(string text, string name)
        {
            barcodename.Content = name;
            GenerateQRCode(text);
        }
        public QRCodeDialog(string text,string name)
        {
            initQrDlg();
            setBarCode(text, name);
        }
      
        public bool deactivated=false;
        private void PopupWindow_Deactivated(object? sender, EventArgs e)
        {
            this.Hide(); // 当窗口失去焦点时关闭窗口  
            deactivated = true;
        }
        private void GenerateQRCode(string text)
        {
           
            BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Width = 300,
                    Height = 300
                },
                Renderer = new BitmapRenderer()
            };

            var bitmap = writer.Write(text);
            qrCodeImage.Source = Imaging.CreateBitmapSourceFromHBitmap(
                bitmap.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

        }
    }
}
